package com.mall507.fivepeaks.activity.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.comn.model.ConvenientLifeErShouMaiMaiInfo;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.xdialog.XAddressDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/main/MainErShouQiuGouInfoUI")
public class MainErShouQiuGouInfoUI extends BaseUI {
    @Autowired(name = "id")
    String nid;
    @ViewInject(R.id.head_banner)
    BGABanner head_banner;
    @ViewInject(R.id.time)
    TextView time;
    @ViewInject(R.id.contacts)
    TextView contacts;
    @ViewInject(R.id.phone)
    TextView phone;
    @ViewInject(R.id.describe)
    TextView describe;
    @ViewInject(R.id.village)
    TextView village;
    @ViewInject(R.id.titles)
    TextView titles;
    @ViewInject(R.id.type)
    TextView type;
    @ViewInject(R.id.call)
    Button call;
    @ViewInject(R.id.dao)
    Button dao;
    String Address = "";
    String callNumber = "";
    String Addresslat = "";
    String Addresslng = "";

    @Override
    protected void onCreateBase() {
        ARouter.getInstance().inject(this);
        setContentView(R.layout.ui_main_ershou_qiugou_info);
        getTitleView().setContent("求购详情");
        showLoding();
        loadNetWorkData();
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = phone.getText().toString();
                callNumber = str.substring(5,str.length());
                call(callNumber);
            }
        });
        dao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new XAddressDialog().show(MainErShouQiuGouInfoUI.this, Address, Addresslng,Addresslat, new XAddressDialog.XUserMainSignRewardDialogCallBack() {
                    @Override
                    public void confirm() {

                    }

                    @Override
                    public void cancel() {

                    }
                });
            }
        });
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
    }

    private void loadNetWorkData() {
        showLoding();
        BaseHttp.getInstance().query("release/releaseDetails", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    dismissLoding();
                    JSONObject list = response.optJSONObject("data");
                    titles.setText(list.optString("title"));
                    time.setText("发布：" + list.optString("posttime")); //发布时间
                    type.setText("二手类型：" + getIntent().getStringExtra("type")); //二手类型
                    Address =  list.optString("address");
                    Addresslat = list.optString("lat");
                    Addresslng = list.optString("lng");
                    village.setText("所在地址：" + list.optString("address"));
                    describe.setText(list.optString("content"));
                    contacts.setText("联系人：" + list.optString("contacts"));
                    phone.setText("联系电话：" + list.optString("tel"));
                }
                dismissLoding();
            }
        });
    }
    
    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
//            params.put("id", nid);
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    private void call(String phoneNum){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
